﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Model;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using GameHelp;
using Common;
using System.Threading;
using System.Runtime.InteropServices;

namespace TdyxAutoBot
{
    static class Program
    {
        static StreamWriter logWriter = null;
        static object txtMessageLocker = new object();

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0 && args[0] == "-c")
            {
                NativeMethods.AllocConsole();
            }
            Init();
            Manager.Init();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            AccountForm accountForm = new AccountForm();
            //MainForm mainForm = new MainForm();
            MainForm mainForm = new MainForm();
            Application.Run(mainForm);
        }

        static void Init()
        {
            FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\log.txt");

            //if (file.Exists && file.Length > 1024 * 1024 * 10)
            if (file.Exists)
            {
                try
                {
                    file.Delete();
                }
                catch
                {
                    MessageBox.Show("日志文件删除失败了！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }
            }

            FileStream stream = file.Open(FileMode.Append, FileAccess.Write, FileShare.Read);
            logWriter = new StreamWriter(stream);
            SetConsoleCtrlHandler(cancelHandler, true);
            Application.ApplicationExit += Application_ApplicationExit;
        }

        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            logWriter.Flush();
            logWriter.Close();
        }

        //打印助手提示信息
        public static void Log(string message)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(delegate(object o)
            {
                lock (txtMessageLocker)
                {
                    logWriter.WriteLine(message);
                }
            }));

            thread.Start();
        }

        public delegate bool ControlCtrlDelegate(int CtrlType);
        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerRoutine, bool Add);
        private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerRoutine);

        public static bool HandlerRoutine(int CtrlType)
        {
            //switch (CtrlType)
            //{
            //    case 0:
            //        Console.WriteLine("0工具被强制关闭"); //Ctrl+C关闭  
            //        break;
            //    case 2:
            //        Console.WriteLine("2工具被强制关闭");//按控制台关闭按钮关闭  
            //        break;
            //}
            Application_ApplicationExit(null, null);
            return false;
        }
    }
}
